package com.lei.lib_croe.http.product;

import com.lei.lib_croe.http.factory.HttpJiek;
import com.lei.lib_croe.http.token.ToKenInterceptor;

import java.util.concurrent.TimeUnit;

import okhttp3.OkHttpClient;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;

public class NetWorkProduct implements IRetrofitProduct {
    private Retrofit retrofit;
    @Override
    public void create() {
        OkHttpClient client = new OkHttpClient.Builder()
                .readTimeout(5, TimeUnit.SECONDS)
                .writeTimeout(5, TimeUnit.SECONDS)
                .connectTimeout(5, TimeUnit.SECONDS)
                .addNetworkInterceptor(new HttpLoggingInterceptor().setLevel(HttpLoggingInterceptor.Level.BODY))
                .addInterceptor(new ToKenInterceptor())
                .build();
        retrofit = new Retrofit.Builder()
                .baseUrl(HttpJiek.HTTP_CHUAN)
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                .addConverterFactory(GsonConverterFactory.create())
                .client(client)
                .build();
    }

    @Override
    public Retrofit getRerofit() {
        if (retrofit == null){
            create();
        }
        return retrofit;
    }
}